package com.tionsy.springbootcomponents.proxy.jdk;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

/**
 * <p></p>
 * <p>
 * <PRE>
 * <BR>    修改记录
 * <BR>-----------------------------------------------
 * <BR>    修改日期         修改人          修改内容
 * </PRE>
 *
 * @author tianyun
 * @version 1.0
 * @date Created in 2021年05月23日 09:17
 * @since 1.0
 */
public class JdkProxy implements InvocationHandler {

    private TicketPerson person;

    public JdkProxy(TicketPerson person){
        this.person = person;
    }

    public TicketPerson createInstance(){
        ClassLoader classLoader = person.getClass().getClassLoader();
        Class<?>[] interfaces = person.getClass().getInterfaces();
        return (TicketPerson) Proxy.newProxyInstance(classLoader, interfaces, this);
    }

    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        System.out.println("黄牛买票");
        Object result = method.invoke(person, args);
        System.out.println("出票成功");
        return result;
    }
}
